﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Xml;
using CapstoneProjectAEReader.Common.Exceptions;

namespace CapstoneProjectAEReader.Common.Utility
{
    public static class Utilities
    {
        #region Attributes

        private const string FileNotFound = "fileNotFound";

        #endregion

        #region Methods

        /// <summary>
        /// Load xml to get controller path
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public static Dictionary<string,string> LoadXml(string fileName)
        {
            Dictionary<string, string> configInfo;

            try
            {
                var xdoc = new XmlDocument();
                xdoc.Load(fileName);
                var xNodes = xdoc.SelectSingleNode(Constants.PathToRoot);
                configInfo = new Dictionary<string, string>();
                foreach (XmlNode node in xNodes)
                {
                    if (!configInfo.ContainsKey(node.Attributes[0].Value))
                    {
                        configInfo.Add(node.Attributes[0].Value.Trim(), node.Attributes[1].Value.Trim());
                    }
                }
            }
            catch (Exception ex)
            {
                throw new AeException(Constants.ExceptionCode.Function,String.Format(ResourceReader.GetResourceContent(FileNotFound),fileName),ex);
            }
            return configInfo;
        }
 
        public static object GetReadingController(Type baseType, object[] param)
        {
            object readerObject = new object();
            ConstructorInfo ctor = baseType.GetConstructor(new[] { typeof(string) });
            readerObject = ctor.Invoke(new object[] { param[0] });
            return readerObject;
        }

        /// <summary>
        /// Check file exist or not
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public static bool CheckFileExist(string fileName)
        {
            return File.Exists(fileName);
        }

        /// <summary>
        /// Copy File
        /// </summary>
        /// <param name="sourcePath"></param>
        /// <param name="destPath"></param>
        public static void CopyFile(string sourcePath, string destPath)
        {
            string folderPath = Path.GetDirectoryName(destPath);
            if (!Directory.Exists(folderPath))
            {
                Directory.CreateDirectory(folderPath);
            }
            File.Copy(sourcePath,destPath, true);
        }

        /// <summary>
        /// Auto Generate 5 letters string
        /// </summary>
        /// <returns></returns>
        public static string GenerateRandomString()
        {
            char[] rtString = new char[5];
            Random rd = new Random();

            for (int i = 0; i < 5; i++)
            {
                rtString[i] = Constants.AllowedChars[rd.Next(0, Constants.AllowedChars.Length)];
            }

            return new string(rtString);
        }

        public static string GetCurrentLocation()
        {
            string path = System.Reflection.Assembly.GetAssembly(typeof(Utilities)).Location;
            return Path.GetDirectoryName(path);
        }

        public static void ShowSuccessfulMesbox(string message)
        {
            MessageBox.Show(message, Constants.ApplicationTitle, MessageBoxButton.OK, MessageBoxImage.Information);
        }

        public static void ShowWarningMesbox(string message)
        {
            MessageBox.Show(message, Constants.ApplicationTitle, MessageBoxButton.OK, MessageBoxImage.Warning);
        }

        public static void ShowErrorMesBox(string message)
        {
            MessageBox.Show(message, Constants.ApplicationTitle, MessageBoxButton.OK, MessageBoxImage.Error);
        }

        public static MessageBoxResult ShowConfirmationMesBox(string message)
        {
            return MessageBox.Show(message, Constants.ApplicationTitle, MessageBoxButton.OKCancel, MessageBoxImage.Question);
        }

        public static string ReturnGroupInText(List<string> inputList )
        {
            return String.Join(Constants.CommonCharacters.Comma, inputList);
        }

        public static void BuildDirectoryStructure()
        {
            string rootDir = GetCurrentLocation() + Constants.CommonCharacters.SlashString +
                             Constants.StorageStructure.RootFolder;
            string bookDir = GetCurrentLocation() + Constants.CommonCharacters.SlashString + Constants.StorageStructure.EbookFolder;
            string tempDir = GetCurrentLocation() + Constants.CommonCharacters.SlashString + Constants.StorageStructure.TempFolder;
            if (!Directory.Exists(rootDir))
            {
                Directory.CreateDirectory(rootDir);
            }
            if(!Directory.Exists(bookDir))
            {
                Directory.CreateDirectory(bookDir);
            }
            if(!Directory.Exists(tempDir))
            {
                Directory.CreateDirectory(tempDir);
            }
        }
        #endregion
    }
}
